package com.proton.carepatchtemp.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.activity.DeviceDetialActivity;
import com.proton.carepatchtemp.adapterholder.DeviceManagerHolder;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.table.Device;
import com.proton.carepatchtemp.utils.AppUtils;
import com.proton.carepatchtemp.utils.NetUtils;
import com.proton.carepatchtemp.utils.TastyUtils;
import com.proton.carepatchtemp.utils.net.DeviceCenter;
import com.sdsmdg.tastytoast.TastyToast;

import java.util.List;

/**
 * Created by MoonlightSW on 2016/12/8.
 */

public class DeviceManagerAdapter extends RecyclerView.Adapter<DeviceManagerHolder> {

    private Context mContext;
    private List<Device> mDevices;
    private LayoutInflater mInflater;

    public DeviceManagerAdapter(Context context, List<Device> devices) {
        this.mContext = context;
        this.mDevices = devices;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public DeviceManagerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_device_manager, parent, false);
        DeviceManagerHolder managerHolder = new DeviceManagerHolder(view);
        return managerHolder;
    }

    @Override
    public void onBindViewHolder(DeviceManagerHolder holder, int position) {
        if (null == mDevices) {
            return;
        }
        Device temp = mDevices.get(position);

        //升级提示
        if (AppUtils.compareVersion(temp.getVersion(), "1.0.1") > 0) {
            if (null != VCare.get().getFirmWare() && AppUtils.compareVersion(temp.getVersion(), VCare.get().getFirmWare().getVersion()) < 0) {
                holder.isNeedUpdate.setVisibility(View.VISIBLE);
            } else {
                holder.isNeedUpdate.setVisibility(View.GONE);
            }
        } else {
            holder.isNeedUpdate.setVisibility(View.GONE);
        }

        holder.mDevice_name_upper.setText(temp.getBtaddress().substring(12));
//        holder.mTv_device_from.setText(temp.getCreatorname());
        holder.mIv_delete.setOnClickListener(view -> {
            if (NetUtils.noNet()) {
                TastyUtils.makeTextShort(R.string.have_no_netconnect, TastyToast.CONFUSING);
                return;
            }
            holder.mSwipe_refresh_layout.quickClose();
            DeviceCenter.delete(String.valueOf(temp.getId()));
            mDevices.remove(position);
            notifyDataSetChanged();
            if (0 == mDevices.size()) {
                DeviceCenter.getList();
            }
        });

        holder.mLinearLayout.setOnClickListener(v -> {
            Intent goDvcDetial = new Intent(mContext, DeviceDetialActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(Extras.DEVICE_DETIAL, temp);
            goDvcDetial.putExtras(bundle);
            mContext.startActivity(goDvcDetial);
        });

    }

    @Override
    public int getItemCount() {
        return null == mDevices ? 0 : mDevices.size();
    }
}
